package com.example.zhinongbao.presenter;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.os.Handler;
import android.os.Looper;

import com.example.zhinongbao.model.Message;
import com.example.zhinongbao.provider.ZhinongbaoContract;

import java.util.ArrayList;
import java.util.List;

public class MessagePresenter {

    public interface View {
        void showMessages(List<Message> messages);
        void onMessageSent();
        void onMessageReceived(Message message);
    }

    private final View view;
    private final ContentResolver contentResolver;
    private final Handler mainHandler = new Handler(Looper.getMainLooper());

    public MessagePresenter(View view, Context context) {
        this.view = view;
        this.contentResolver = context.getContentResolver();
    }

    public void loadMessages() {
        new Thread(() -> {
            List<Message> messages = new ArrayList<>();
            try (Cursor cursor = contentResolver.query(
                    ZhinongbaoContract.Messages.CONTENT_URI,
                    null, null, null,
                    ZhinongbaoContract.Messages.COLUMN_TIMESTAMP + " ASC"
            )) {
                while (cursor != null && cursor.moveToNext()) {
                    long id = cursor.getLong(cursor.getColumnIndexOrThrow(ZhinongbaoContract.Messages._ID));
                    String content = cursor.getString(cursor.getColumnIndexOrThrow(ZhinongbaoContract.Messages.COLUMN_CONTENT));
                    long timestamp = cursor.getLong(cursor.getColumnIndexOrThrow(ZhinongbaoContract.Messages.COLUMN_TIMESTAMP));
                    int isSent = cursor.getInt(cursor.getColumnIndexOrThrow(ZhinongbaoContract.Messages.COLUMN_IS_SENT));

                    messages.add(new Message(id, content, new java.util.Date(timestamp), isSent == 1));
                }
            }

            mainHandler.post(() -> view.showMessages(messages));
        }).start();
    }

    public void sendMessage(String content) {
        new Thread(() -> {
            ContentValues values = new ContentValues();
            values.put(ZhinongbaoContract.Messages.COLUMN_CONTENT, content);
            values.put(ZhinongbaoContract.Messages.COLUMN_IS_SENT, 1); // 发送
            values.put(ZhinongbaoContract.Messages.COLUMN_TIMESTAMP, System.currentTimeMillis());

            contentResolver.insert(ZhinongbaoContract.Messages.CONTENT_URI, values);

            mainHandler.post(() -> {
                view.onMessageSent();
                // 模拟5秒后收到回复
                mainHandler.postDelayed(() -> {
                    Message reply = new Message("已收到: " + content, false);
                    receiveMessage(reply);
                }, 5000);
            });
        }).start();
    }

    private void receiveMessage(Message message) {
        new Thread(() -> {
            ContentValues values = new ContentValues();
            values.put(ZhinongbaoContract.Messages.COLUMN_CONTENT, message.getContent());
            values.put(ZhinongbaoContract.Messages.COLUMN_IS_SENT, 0); // 接收
            values.put(ZhinongbaoContract.Messages.COLUMN_TIMESTAMP, System.currentTimeMillis());

            contentResolver.insert(ZhinongbaoContract.Messages.CONTENT_URI, values);

            mainHandler.post(() -> view.onMessageReceived(message));
        }).start();
    }
}